package com.ali.common;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class StringUtil {
//String

    /**
     * 转大写
     */
    public static String toUpperCase(String str) {
        return str.toUpperCase();
    }

    /**
     * 转小写
     */
    public static String toLowerCase(String str) {
        return str.toLowerCase();
    }

    /**
     * 获取字符串长度
     */
    public static int getLength(String str) {
        return str.length();
    }

    /**
     * 判断是否为空字符串
     */
    public static boolean isEmpty(String str) {
        return str.isEmpty();
    }

    /**
     * 判断是否为空格字符串
     */
    public static boolean isBlank(String str) {
        return str.isBlank();
    }

    /**
     * 判断是否为同一字符串
     */
    public static boolean equals(String str1, String str2) {
        return str1.equals(str2);
    }

    /**
     * 替换字符串中的内容
     */
    public static String replace(String str, String replaceOldStr, String replaceNewStr) {
        return str.replace(replaceOldStr, replaceNewStr);
    }

    /**
     * 分割字符串
     */
    public static String[] split(String str, String splitStr) {
        return str.split(splitStr);
    }

//String与Char Array

    /**
     * String转化为Char Array
     */
    public static char[] stringToCharArray(String str) {
        return str.toCharArray();
    }

    /**
     * Char Array转化为String
     */
    public static String charArrayToString(char[] cs) {
        return String.valueOf(cs);
    }

    /**
     * 复制Char Array转化为String
     */
    public static String copyCharArrayToString(char[] cs) {
        return String.copyValueOf(cs);
    }

//String与ByteArrayInputStream

    /**
     * String转化为ByteArrayInputStream
     */
    public static ByteArrayInputStream stringToByteArrayInputStream(String str) {
        try {
            byte[] strByte = str.getBytes(StandardCharsets.UTF_8);
            return new ByteArrayInputStream(strByte);
        } catch (Exception e) {
            e.fillInStackTrace();
        }

        return null;
    }

    /**
     * ByteArrayInputStream转化为String
     */
    public static String byteArrayInputStreamToString(ByteArrayInputStream byteArrayInputStream) {
        try {
            byte[] bytes = new byte[byteArrayInputStream.available()];
            byteArrayInputStream.read(bytes);
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.fillInStackTrace();
        }

        return null;
    }
}
